package com.woniu113.junk.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu113.junk.order.client.SiteClient;
import com.woniu113.junk.order.client.UserClient;
import com.woniu113.junk.order.client.dto.AddressInfoDTO;
import com.woniu113.junk.order.client.dto.AdminInfoDTO;
import com.woniu113.junk.order.client.dto.GoodsDTO;
import com.woniu113.junk.order.client.dto.UserInfoDTO;
import com.woniu113.junk.order.client.form.IntegralForm;
import com.woniu113.junk.order.dto.AllGoodsOrderDTO;
import com.woniu113.junk.order.dto.GoodsOrderDTO;
import com.woniu113.junk.order.dto.GoodsOrderInfoDTO;
import com.woniu113.junk.order.dto.PerformanceInfoDTO;
import com.woniu113.junk.order.exception.GoodsOrderException;
import com.woniu113.junk.order.model.GoodsOrderInfo;
import com.woniu113.junk.order.mapper.GoodsOrderInfoMapper;
import com.woniu113.junk.order.model.PerformanceInfo;
import com.woniu113.junk.order.param.AddGoodsOrderParam;
import com.woniu113.junk.order.param.FindGoodsByAdminParam;
import com.woniu113.junk.order.param.FindGoodsOrderParam;
import com.woniu113.junk.order.param.FindGoodsParam;
import com.woniu113.junk.order.service.GoodsOrderInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu113.junk.order.util.Data;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 蜡笔没了小新
 * @since 2024年10月16日
 */
@Service
public class GoodsOrderInfoServiceImpl extends ServiceImpl<GoodsOrderInfoMapper, GoodsOrderInfo> implements GoodsOrderInfoService {
    @Resource
    private GoodsOrderInfoMapper goim;
    @Resource
    private UserClient userClient;
    @Resource
    private SiteClient siteClient;
    @Override
    public void buy(AddGoodsOrderParam param) {
        Integer goodsId = param.getGoodsId();
        Integer addressId = param.getAddressId();
        Integer userId = param.getUserId();
        System.out.println("userId"+userId);
        Integer number = param.getNumber();
        GoodsDTO goodsDTO = siteClient.findByGoodsId(goodsId).getData();
        UserInfoDTO userInfoDTO = userClient.selectIntegral(userId).getData();
        System.out.println("**"+userInfoDTO);
        AddressInfoDTO addressInfoDTO = userClient.addressById(addressId).getData();
        Integer integral = userInfoDTO.getIntegral();
        Integer needIntegral = goodsDTO.getNeedIntegral();
        //1.判断商品是否可以购买
        if (goodsDTO==null||goodsDTO.getState()==2||goodsDTO.getUpNumber()<number){
            throw new GoodsOrderException("商品无法购买",5001);
        }
        //2.锁定商品库存
        //3.判断用户是否可以购买商品
        if (integral-needIntegral*number<0) throw new GoodsOrderException("积分不足",5002);
        if (addressInfoDTO==null||addressInfoDTO.getUserId()!=userId) throw new GoodsOrderException("地址不存在",5003);
        //4.生成订单及库存更新
        siteClient.updateNumberByGoodsId(goodsId,number);
        GoodsOrderInfo orderInfo = new GoodsOrderInfo();
        orderInfo.setUserId(userId);
        orderInfo.setAddressId(addressId);
        orderInfo.setGoodsId(goodsId);
        orderInfo.setState(Data.GOODS_STATE_NORMAL);
        orderInfo.setOrderState(Data.GOODS_ORDER_STATE_WAITING);
        orderInfo.setNumber(number);
        orderInfo.setTotalIntegral(needIntegral*number);
        orderInfo.setTime(LocalDateTime.now());
        String goodsInfo = goodsDTO.getGoodsImage()+","+goodsDTO.getGoodsName();
        orderInfo.setGoodsInfo(goodsInfo);
        goim.insert(orderInfo);
        //减少积分
        IntegralForm form = new IntegralForm();
        form.setUserId(userId);
        form.setIntegral(needIntegral*number);
        userClient.reduceIntegralById(form);
    }

    @Override
    public Page<GoodsOrderInfo> findByUserId(FindGoodsOrderParam param) {
        Page<GoodsOrderInfo> page = new Page<>(param.getPageNum(), param.getPageSize());
        QueryWrapper<GoodsOrderInfo> queryWrapper = new QueryWrapper<>();
        if (param.getUserId() != null){
            queryWrapper.eq("user_id",param.getUserId());
        }
        if (param.getOrderState() != null){
            queryWrapper.eq("order_state",param.getOrderState());
        }
        queryWrapper.eq("state",Data.GOODS_STATE_NORMAL);
        return goim.selectPage(page,queryWrapper);
    }

    @Override
    public GoodsOrderDTO findByUser(FindGoodsParam param) {
        int offset = (param.getPageNum() - 1) * param.getPageSize();
        QueryWrapper<GoodsOrderInfo> queryWrapper = new QueryWrapper<>();
        if (param.getUserId() != null){
            queryWrapper.eq("user_id",param.getUserId());
        }
        if (param.getOrderState() != null){
            queryWrapper.eq("order_state",param.getOrderState());
        }
        queryWrapper.eq("state",Data.GOODS_STATE_NORMAL);
        long count = goim.selectCount(queryWrapper);
        queryWrapper.last("LIMIT " + offset + ", " + param.getPageSize());
        List<GoodsOrderInfo> infos = goim.selectList(queryWrapper);
        GoodsOrderDTO dto = new GoodsOrderDTO();
        dto.setList(infos);
        dto.setTotal(Integer.valueOf((int) count));
        return dto;
    }

    @Override
    public AllGoodsOrderDTO findByAdmin(FindGoodsByAdminParam param) {
        int offset = (param.getPageNum() - 1) * param.getPageSize();
        QueryWrapper<GoodsOrderInfo> queryWrapper = new QueryWrapper<>();
        if (param.getOrderState() != null){
            queryWrapper.eq("order_state",param.getOrderState());
        }
        if (param.getOrderId() != null){
            queryWrapper.eq("order_id",param.getOrderId());
        }
        queryWrapper.eq("state",Data.GOODS_STATE_NORMAL);
        long count = goim.selectCount(queryWrapper);
        queryWrapper.last("LIMIT " + offset + ", " + param.getPageSize());
        List<GoodsOrderInfo> infos = goim.selectList(queryWrapper);

        ArrayList<GoodsOrderInfoDTO> goodsOrderInfoDTOS = new ArrayList<>();
        for (GoodsOrderInfo goodsOrderInfo : infos) {
            UserInfoDTO userInfo = userClient.selectUserById(goodsOrderInfo.getUserId()).getData();
            GoodsOrderInfoDTO goodsOrderInfoDTO = BeanUtil.toBean(goodsOrderInfo, GoodsOrderInfoDTO.class);
            goodsOrderInfoDTO.setUserName(userInfo.getUserName());
            goodsOrderInfoDTO.setUserPhone(userInfo.getUserPhone());
            AddressInfoDTO addressInfoDTO = userClient.addressById(goodsOrderInfo.getAddressId()).getData();
            goodsOrderInfoDTO.setAddressName(addressInfoDTO.getAddressName());
            goodsOrderInfoDTOS.add(goodsOrderInfoDTO);
        }
        AllGoodsOrderDTO dto = new AllGoodsOrderDTO();
        dto.setList(goodsOrderInfoDTOS);
        dto.setTotal(Integer.valueOf((int) count));
        return dto;
    }
}
